<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/22
 * Time: 14:12
 */

namespace common\sources\read\taskorder;

use common\models\craftsman\TaskOrder;
use Yii;

class TaskOrderAccess implements ITaskOrderAccess
{
    /**
     * 获取主任务订单下面的状态子订单 统计
    **/
    public function getTaskOrderStatusCount($task_commod_id,$status)
    {
        return TaskOrder::find()
            ->where(" task_commod_id = ".$task_commod_id." AND order_status = ".$status)
            ->count();
    }

    //获取主任务订单下面的状态子订单
    public function getTaskOrderByStatus($task_commod_id,$status,$page =1,$limit =10)
    {
        return TaskOrder::find()
            ->select("id,plat_type,task_type,task_commod_id,buy_id,task_order_num,bus_id,tbname,create_time,pay_order_num,order_status,refund_amount,express_num,parise_url,tao_parise_url")
            ->where(" task_commod_id = ".$task_commod_id." AND order_status = ".$status)
            ->orderBy("create_time desc")
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->asArray()
            ->all();
    }

    public function getTaskOrdersByIds($ids,$status)
    {
        return TaskOrder::find()
            ->select("plat_type,task_type,task_commod_id,buy_id,tb_id,bus_id")
            ->where(" id in(".$ids.") AND order_status = ".$status)
            ->asArray()
            ->all();
    }

    public function getTaskOrderDetial($task_order_id)
    {
        return TaskOrder::find()
            ->select("plat_type,task_type,task_commod_id,buy_id,tb_id,bus_id,express_num,tbname,order_status,express_num,tbname,title,price,num,actual_price,main_url1,port_type,keyword_url,search_time,bus_pay_url,serial_num,
            purchase_num,pay_order_num,addr_url,pay_url,order_status,advance_amount,refund_amount,actual_refunds,commission,receive_name,receive_phone,pro_name,pro_code,city_name,city_code,area_name,area_code,addr,browse_time,order_time,
            merchant_time,favorable_time,plat_return_time,confirm_time,reject_time,give_up_time,cancel_time,parise_url,tao_parise_url,return_type,task_order_num,create_time")
            ->where(['id'=> $task_order_id])
            ->asArray()
            ->one();
    }

    public function getTaskOrderCount($task_commod_ids,$order_status)
    {
        $sql = " task_commod_id in (".$task_commod_ids.") AND order_status = ".$order_status;
        return TaskOrder::find()
                ->select("count(*) as count,task_commod_id")
                ->where($sql)
                ->groupBy("task_commod_id")
                ->asArray()
                ->all();
    }
    //统计某些子订单的 某些状态
    public function getHavingTaskOrderCount($task_commod_ids, $order_statues)
    {
        $sql = " task_commod_id in (".$task_commod_ids.") AND order_status in ( ".$order_statues ." )";
        return TaskOrder::find()
            ->select("count(*) as count,task_commod_id")
            ->where($sql)
            ->groupBy("task_commod_id")
            ->asArray()
            ->all();
    }
    public function getTaskOrderSuccess($task_commod_ids,$order_status)
    {
        $sql = " task_commod_id = (".$task_commod_ids.") AND order_status = ".$order_status;
        return TaskOrder::find()
            ->where($sql)
            ->count();
    }
    //统计详情页面 状态值 进行中的
    public function getHavingCountTaskOrder($task_commod_ids, $order_statues)
    {
        $sql = " task_commod_id = ".$task_commod_ids." AND order_status in ( ".$order_statues ." )";
        return TaskOrder::find()
            ->select("count(*) as count,task_commod_id")
            ->where($sql)
            ->count();
    }

    //统计商家订单数量
    public function getTaskOrderBusCount($task_commod_id, $task_order_status)
    {
        $sql = " task_commod_id = ".$task_commod_id." AND order_status = ".$task_order_status ;
        return TaskOrder::find()
            ->where($sql)
            ->count();
    }
    //统计商家订单数量
    public function getBusinessTaskOrderCount($bus_id, $task_order_status)
    {
        $sql = " bus_id = ".$bus_id." AND order_status in ( ".$task_order_status." )" ;
        return TaskOrder::find()
            ->where($sql)
            ->count();
    }

    //统计商家子订单状态数量
    public function getBusTaskOrderCount($data)
    {
        $where = $this->getWhere($data);
        $taskOrder = TaskOrder::find()
            ->alias("t");
        if($data['search_type'] ==1 && !empty($data['search_text']) && isset($data['search_text']))
        {
            $taskOrder->leftJoin("task_commod c","t.task_commod_id = c.id");
        }
       return $taskOrder->where($where)
                  ->count();

    }
    //获取商家子订单列表
    public function getBusTaskOrderList($data)
    {
        $where = $this->getWhere($data);
        $taskOrder = TaskOrder::find()
                ->alias("t")
                ->select("t.id,t.shop_name,t.tbname,t.main_url1,t.task_type,t.create_time,t.task_order_num,t.actual_price,t.express_num,t.colour,t.addr_url,t.return_type,
                        t.size,t.plat_type,t.actual_price,t.title,t.task_order_num,t.pay_order_num,t.order_status,t.refund_amount,t.pay_url,t.actual_refunds,task_commod_id");
        if($data['search_type'] ==1 && !empty($data['search_text']) && isset($data['search_text']))
        {
            $taskOrder->leftJoin("task_commod c","t.task_commod_id = c.id");
        }
        return $taskOrder->where($where)
                        ->offset(($data['page_num']-1)*$data['limit'])
                        ->limit($data['limit'])
                        ->asArray()
                        ->all();
    }
    //获取条件
    private function getWhere($data)
    {
        $sql = " t.bus_id =".$data['bus_id'];
        if(!empty($data['order_status']) && isset($data['order_status']))
        {
            if($data['order_status'] == 3)
            {
                $sql .= " AND t.order_status in ( 2,3)";
            }else
            {
                $sql .= " AND t.order_status in (".$data['order_status'] ." )";
            }

        }
        if(!empty($data['plat_type']) && isset($data['plat_type']))
        {
            $sql .= " AND t.plat_type =".$data['plat_type'];
        }
        if(!empty($data['shop_id']) && isset($data['shop_id']))
        {
            $sql .= " AND t.shop_id =".$data['shop_id'];
        }
        if(!empty($data['task_type']) && isset($data['task_type']))
        {
            $sql .= " AND t.task_type =".$data['task_type'];
        }
        if(!empty($data['search_type']) && isset($data['search_type']))
        {
            if($data['search_type'] ==1 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND c.task_id =".$data['search_text'];
            }else if($data['search_type'] == 2 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.task_order_num =".$data['search_text'];
            }else if($data['search_type'] == 3 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.pay_order_num =".$data['search_text'];
            }else if($data['search_type'] == 4 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.tbname ='".$data['search_text']."'";
            }else if($data['search_type'] == 5 && !empty($data['search_text']) && isset($data['search_text']))
            {
                $sql .= " AND t.title like '%".$data['search_text']."%'";
            }
        }
        return $sql;
    }
    public function getTaskOrderDetailCount($data)
    {
        $where = $this->getTaskOrderDetail($data);
        return TaskOrder::find()
                ->where($where)
                ->count();
    }
    public function getTaskOrderDetailList($data)
    {
        $where = $this->getTaskOrderDetail($data);
        return TaskOrder::find()
            ->select("id,shop_name,tbname,main_url1,task_type,create_time,task_order_num,refund_amount,express_num,main_url1,colour,order_status,
                        size,plat_type,task_type,actual_price,title,task_order_num,pay_order_num,keywords")
            ->where($where)
            ->orderBy("create_time desc")
            ->offset(($data['page_num']-1)*$data['limit'])
            ->limit($data['limit'])
            ->asArray()
            ->all();
    }
    public function getTaskOrderDetail($data)
    {
        $sql = " bus_id = ".$data['bus_id'];
        if(!empty($data['task_commod_id']) && isset($data['task_commod_id']))
        {
            $sql .= " AND task_commod_id =".$data['task_commod_id'];
        }
        if($data['task_order_status'] ==1)
        {
            $sql .= " AND order_status =1";
        }else if($data['task_order_status'] == 2)
        {
            $sql .= " AND order_status  in (2,3,4)";
        }else if($data['task_order_status'] == 3)
        {
            $sql .= " AND order_status in (1,2,3,4,5,6,7,9)";
        }else if($data['task_order_status'] == 4)
        {
            $sql .= " AND order_status  in (8,12,13)";
        }
        if(!empty($data['search_text']) && isset($data['search_text']))
        {
            $sql .= " AND (ww_name = '".$data['search_text']."' or pay_order_num ='".$data['search_text']."')";
        }
        return $sql;
    }

    //按照天统计总人数
    public function getTaskOrderByBuyIdCount($data)
    {
        $where = " buy_id in (".$data['buy_ids'].")  AND order_status in (".$data['order_status'].")";
        $sql = "select  from_unixtime(create_time, '%Y-%m-%d') AS `days`,count(buy_id) as buy_count,count(id) as task_ount from  task_order where ".$where." GROUP BY `days` ORDER BY `days` DESC";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    //按天统计接单人数和接单数量
    public function getTaskOrderByBuyID($data)
    {
        $where = " buy_id in (".$data['buy_ids'].")  AND order_status in (".$data['order_status'].")";
        $sql = "select  from_unixtime(create_time, '%Y-%m-%d') AS `days`,count(buy_id) as buy_count,count(id) as task_ount from  task_order where ".$where." GROUP BY `days` ORDER BY `days` DESC";
        return Yii::$app->db->createCommand($sql)->queryAll();

    }

    //获取任务排名奖励
    public function getTaskOrderSort($data)
    {
        $sql = "select sum(b.income) as income,b.task_order_id as task_order_id,t.buy_id as buy_id
                from buy_pepper_record b
                LEFT JOIN buyuser  u on b.buy_id = u.id
                LEFT JOIN task_order t on b.task_order_id = t.id
                where from_unixtime(b.create_time,'%Y-%m-%d') = '".$data['days']."' and b.type = 7 AND b.buy_id = ".$data['buy_id']."
                GROUP BY t.buy_id
                order by b.income desc;";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }
    //任务单数量展示
    public function getTaskOrderPepper($data)
    {
        $sql = "select t.actual_refunds as actual_refunds,t.task_order_num as task_order_num, b.income as income
                from task_order t
                LEFT JOIN buy_pepper_record  b
                on t.id = b.task_order_id 
                where b.buy_id = ".$data['buy_id']." and b.type =7
                ;";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }



}